import {
  request
} from '../../utils/request'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    typeList: [{
        name: '活动'
      },
      {
        name: '比赛'
      }
    ],
    dataList: [],
    selectDate: '',
    selectDateFormat: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  onChange({
    detail
  }) {
    this.setData({
      currentTab: detail.name
    })
    if (this.data.currentTab == 0) {
      this.getActivityList()
    } else {
      this.getCompetitionList()
    }
  },

  getActivityList() {
    request({
      url: '/mini/api/makeTime/queryActivityList',
      method: 'POST',
      data: {
        activityDate: this.data.selectDate
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          dataList: resdata.data || []
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  getCompetitionList() {
    request({
      url: '/mini/api/makeTime/queryCompetitionList',
      method: 'POST',
      data: {
        competitionDate: this.data.selectDate
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          dataList: resdata.data || []
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  toItem(item) {
    if (this.data.currentTab == 0) {
      wx.navigateTo({
        url: '/packagePlan/activityDetail/activityDetail?id=' +
          item.currentTarget.dataset.item.id
      })
    } else {
      wx.navigateTo({
        url: '/packageSquare/rankingDetail/rankingDetail?id=' +
          item.currentTarget.dataset.item.id
      })
    }
  },

  selectDay(item) {
    if (
      item.detail == null ||
      item.detail == undefined
    ) {
      this.setData({
        selectDate: item.year +
          '-' +
          (item.month < 10 ?
            '0' + item.month :
            item.month) +
          '-' +
          (item.day < 10 ?
            '0' + item.day :
            item.day)
      })
    } else {
      this.setData({
        selectDate: item.detail.year +
          '-' +
          (item.detail.month < 10 ?
            '0' + item.detail.month :
            item.detail.month) +
          '-' +
          (item.detail.day < 10 ?
            '0' + item.detail.day :
            item.detail.day)
      })
    }
    this.setData({
      selectDateFormat: this.data.selectDate.replace(
        /-/g,
        '/'
      )
    })

    if (this.data.currentTab == 0) {
      this.getActivityList()
    } else {
      this.getCompetitionList()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let app = getApp()
    app.getCurrentTabbar(1, this)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})